$(function(){
	//请求数据
	createDetailTable();

	//搜索确认按钮
	$("#confirmBtnD").click(function(){
		createDetailTable(); //请求数据
		mui.back(); //mui 返回
	});


	//选择器
	$(".myPicker").click(function(){
		var self = this;
		var nowDate = new Date();
		//创建普通的popPicker
		var popPicker = new mui.PopPicker();
		var popDataArray = new Array();
		//创建日期形式的Depicker
		var datePicker = new mui.DtPicker({
			type: "date",
			endDate: nowDate
		});
		switch ($(self).attr('id')){
			//开始日期选择器
			case 'selectorBeginDateD':
				datePicker.setSelectedValue(getMonthStartDate());
				datePicker.show(function(items) {
					$("#selectorBeginDateShowD").html(items.value);
					datePicker.dispose();
				});
				break;
			//结束日期选择器
			case 'selectorEndDateD':
				datePicker.show(function(items) {
					$("#selectorEndDateShowD").html(items.value);
					datePicker.dispose();
				});
				break;
			//进出码
			case 'selectorClearD':
				popDataArray = [];//将数组清空
				popDataArray = [{value: '' ,text: '全部'},{value: '1' ,text: '入库'},{value: '0' ,text: '出库'}];
				popPicker.setData(popDataArray);
				popPicker.show(function(items) {
					$("#selectorClearShowD").html(items[0].text).attr('data-value',items[0].value);
					popPicker.dispose();
				});
				break;
			default:
				break;
		}
	});
})

//获取往来明细数据
function createDetailTable(){
	var limit = 20;
	var params = {
		beginDate : $('#selectorBeginDateShow').html(),
		endDate : $('#selectorEndDateShow').html(),
		qryIoType: $('#qryToTypeS').val(),
		qryInOut: $('#selectorClearShowD').attr('data-value'), //清算状态
	}
	var data = JSON.parse(window.localStorage.stockParams);
	//显示当前查询条件
	$("#qryIoTypeD").html(params.qryIoType || '');
	$("#qryInOutD").html($('#selectorClearShowD').html());
	$("#beginDateD").html(params.beginDate || '');
	$("#endDateD").html(params.endDate || '');

	$('#table').html("");
	layui.use('table', function(){
		var table = layui.table;

		//第一个实例
		table.render({
			elem: '#table'
			,limit: limit
			// ,skin: 'line'
			,even: true
			,size: 'sm'
			,height: $(window).height() - $('.head-search').height() - 120
			,url: url1 + Utils.Api.rptMgntKcDetail //数据接口
			,where: {
				accessid: window.localStorage.accessid || '',
				field: 'mainDate',
				direction: 'DESC',
				subjectId: data.subjectId,
				unitId: data.unitId,
				wareId: data.wareId,
				resId: data.resId,
				stateId: data.stateId,
				qryInOut: params.qryInOut || '',  //进出码
				qryIoType: params.qryIoType || '',//单据类型
				qryDateB: params.beginDate || '',
				qryDateE: params.endDate || '',
			}
			,request: {
				pageName: 'pageIndex' //页码的参数名称，默认：page
				,limitName: 'limit' //每页数据量的参数名，默认：limit
			}
			, response: {
				statusName: 'hasError', //数据状态的字段名称，默认：code
				statusCode: false, //成功的状态码，默认：0
				msgName: 'error', //状态信息的字段名称，默认：msg
				countName: 'results', //数据总数的字段名称，默认：count
				dataName: 'rows' //数据列表的字段名称，默认：data
			}
			, page: true //开启分页
			,cols: [[ //表头
				{field: 'mainDate', title: '交易日期',align: 'center', width: 120,templet: function (d) {
					return  !d.summary?dateTimeFormat("yyyy-MM-dd", new Date(d.mainDate)): '<span class="font-bold">' + d.summary+'</span>'
				}},
				{field: 'remark', title: '摘要备注',align: 'center', width: 100},
				{field: 'kcInOut', title: '进出码',align: 'center', width: 60, templet: function(d){
					return !d.summary?d.kcInOut === 0?'出库': '入库': ""
				}},
				{field: 'stockQtySign', title: '进出数量',align: 'center', width: 100, templet: function(d){
					return !d.summary?d.stockQtySign: '<span class="font-bold">' + d.stockQtySign+'</span>'
				}},
				{field: 'costPrice', title: '成本单价',align: 'right', width: 100,templet: function (d) {
					return  !d.summary? Utils.formatMoney(d.costPrice): '<span class="font-bold">' + Utils.formatMoney(d.costPrice)+'</span>'
				}},
				{field: 'costAmountSign', title: '成本金额',align: 'right', width: 100,templet: function (d) {
					return  !d.summary? Utils.formatMoney(d.costAmountSign): '<span class="font-bold">' + Utils.formatMoney(d.costAmountSign)+'</span>'
				}},
				{field: 'unitName', title: '单位',align: 'center', width: 60},
				{field: 'mainTypeName', title: '单据类型',align: 'center', width: 100},
				{field: 'qtySign', title: '交易数量',align: 'center', width: 100, templet: function(d){
					return !d.summary?d.qtySign: '<span class="font-bold">' + d.qtySign+'</span>'
				}},
				{field: 'price', title: '交易单价',align: 'right', width: 100,templet: function (d) {
					return  !d.summary? Utils.formatMoney(d.price): '<span class="font-bold">' + Utils.formatMoney(d.price)+'</span>'
				}},
				{field: 'amountSign', title: '交易金额',align: 'right', width: 100,templet: function (d) {
					return  !d.summary? Utils.formatMoney(d.amountSign): '<span class="font-bold">' + Utils.formatMoney(d.amountSign)+'</span>'
				}},
				{field: 'fzQtySign', title: '辅助数量',align: 'center', width: 100,  templet: function(d){
					return !d.summary?d.fzQtySign: '<span class="font-bold">' + d.fzQtySign+'</span>'
				}},
				{field: 'fzAmountSign', title: '辅助金额',align: 'right', width: 100,templet: function (d) {
					return  !d.summary? Utils.formatMoney(d.fzAmountSign): '<span class="font-bold">' + Utils.formatMoney(d.fzAmountSign)+'</span>'
				}},
				{field: 'fzUnitName', title: '辅助单位',align: 'center', width: 100},
				{field: 'custStr', title: '凭据单号',align: 'center', width: 100},
				{field: 'subjectNick', title: '主体名称',align: 'center', width: 100},
				{field: 'wareName', title: '单元名称',align: 'center', width: 120},
				{field: 'resName', title: '资源名称',align: 'center', width: 100},
				{field: 'stateName', title: '资源状态',align: 'center', width: 100},
				{field: 'mainNo', title: '进出单号',align: 'center', width: 120},
			]]
			,sums: true
		});


	})

}